package com.like.shopmanager;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;

import com.like.entity.EventWrapper;
import com.like.entity.LoginedUser;
import com.like.entity.PushAmount;
import com.like.receivers.PushMessageReceiver;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.xutils.view.annotation.ContentView;
import org.xutils.view.annotation.Event;
import org.xutils.view.annotation.ViewInject;

@ContentView(R.layout.activity_main)
public class MainActivity extends BaseActivity {

    @ViewInject(R.id.title)
    private TextView mTitle;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        LoginedUser user = m.getLoginedUser();
        if (user != null)
            mTitle.setText(user.storeName + "");
    }

    @Override
    protected void onStart() {
        super.onStart();
        EventBus.getDefault().register(this);
    }

    @Override
    protected void onStop() {
        super.onStop();
        EventBus.getDefault().unregister(this);
    }

    @Subscribe(sticky = true)
    public void onGetPushMsg(EventWrapper event) {
        if (EventWrapper.isMatch(event, PushMessageReceiver.TAG_PUSH, false)) {
            PushAmount amount = (PushAmount) event.data;
            Intent intent = null;
            if (amount.opType == PushAmount.TYPE_RECHARGE) {
                intent = new Intent(m.mContext, RechargeActivity.class);
            } else if (amount.opType == PushAmount.TYPE_DEBIT) {
                intent = new Intent(m.mContext, DebitActivity.class);
            }
            if (intent != null)
                startActivity(intent);
        }
    }

    @Event(R.id.debit)
    private void debitClick(View v) {
        Intent intent = new Intent(m.mContext, DebitActivity.class);
        startActivity(intent);
    }

    @Event(R.id.recharge)
    private void rechargeClick(View v) {
        Intent intent = new Intent(m.mContext, RechargeActivity.class);
        startActivity(intent);
    }

    @Event(R.id.chart)
    private void chartClick(View v) {
        Intent intent = new Intent(m.mContext, ChartActivity.class);
        startActivity(intent);
    }

    @Event(R.id.logout)
    private void logoutClick(View v) {
        m.logout();
        Intent intent = new Intent(m.mContext, LoginActivity.class);
        startActivity(intent);
        finish();
    }
}
